<template>
    <div class="subProcess">
        <Tabs class="tabs-layout" v-model="active" @on-click="onTabs">
            <TabPane v-for="(item,index) in list" :label="'Sub-projects'+item.projectId" :name="index" :key="index">
                <subProcessItem v-if="active===index" ref="subProcessItem" :components="components"
                                :isActive="active===index" :row="item" :mainInfo="info" @onMessage="onMessage"/>
                <bottom-handle-wrap v-if="isSHow" class="bottom-handle-wrap">
<!--                    v-has="'capexCallback'"-->
                    <Button  :loading="btnLoading" v-if="canRollback" @click="rollback">{{ $t('回退') }}</Button>
                    <Button @click="$router.go(-1)">{{ $t('取消') }}</Button>
<!--                    v-has="'capexJump'"-->
                    <Button  :loading="btnLoading" v-if="progressCode==='Bidding'" @click="jumpBiddingMethod">{{ $t('跳过') }}</Button>
<!--                    v-has="'capexCache'"-->
                    <Button :loading="btnLoading" style="margin-right: 10px" type="warn" v-if="showCacheBtn" @click="onCache">{{ $t('暂存') }}</Button>
<!--                    v-has="'capexSubmit'"-->
                    <Button  :loading="btnLoading" style="margin-right: 10px" type="primary" @click="onSubmit">{{ $t('提交') }}</Button>
                </bottom-handle-wrap>
            </TabPane>
        </Tabs>
        <jumpBindding :id="id" ref="jumpBidding"/>
        <rollbackModal ref="rollbackModal" :active="progressCode" :id="id"/>
    </div>
</template>

<script>
import subProcessItem from "@/views/devBasic/capex/components/subProcessItem";
import jumpBindding from "@/views/devBasic/capex/components/jumpBindding";
import rollbackModal from "@/views/devBasic/capex/components/rollbackModal";

export default {
    name: "subProcess",
    components: {subProcessItem, jumpBindding, rollbackModal},
    props: ['components', 'info'],
    data() {
        return {
            list: [],
            active: 0,
            toggle: true,
            isSHow: false,
            progressCode: "",
            id: "",
            btnLoading: false,
            canRollback: false,
            showCacheBtn: false
        }
    },
    created() {

        this.list = this.info.capexSubmitPos
    },
    methods: {
        onMessage(data) {
            this.canRollback = false
            this.showCacheBtn = false
            this.id = data.id;
            this.progressCode = data.active
            this.isSHow = data.progressCode !== 'Capitalization Approval'

            // if (data.list.length>1){
            //     this.canRollback = true
            // }

            if (data.list.length  > 1){
                if (data.list[data.list.length - 2]['approval'] == 0 && data.list[data.list.length - 1]['approval'] == 0) {
                    this.canRollback = true
                }
            }

            if (data.cvRepeatTaggingStatus) {
                this.canRollback = false
            }

            //
            // if (data.clockSubProject){
            //   this.isSHow = !data.clockSubProject
            // }
            // if (this.info.bdgApprovalFlag===1) {
            //   this.isSHow = false
            // }
            if (data.displayButton == 1) {
                this.isSHow = false
            }

            this.getCacheBtn(data.active,data.list,data)
        },
        getCacheBtn(active, list,data) {
            const obj = list.find(item => item.key === active)
            if (obj.approval == 0 && active !== 'Spec. In' && !(active ==='Confirm Vendor' && (data.cvRepeatTagging==1||data.cvRepeatTagging==2))) {
                this.showCacheBtn = true
            }
        },
        onTabs() {
            this.isSHow = false
        },
        rollback() {
            this.$refs.rollbackModal.showModal()
        },
        jumpBiddingMethod() {
            this.$refs.jumpBidding.show()
        },
        async onSubmit() {
            const params = await this.$refs.subProcessItem[0].getParams();
            this.approval = params.approval
            if (!params) return;
            delete params.projectOwner
            delete params.submitByStr
            delete params.approval


            this.btnLoading = true
            if (this.approval == 1) {
                this.axios({
                    method: 'post',
                    url: '/dms/capex/approval',
                    data: params
                }).then(res => {
                    if (res.code === 200) {
                        this.$Message.success(res.msg)
                        this.$router.go(-1)
                        // location.reload()
                    } else {
                        this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
                    }
                }).finally(() => {
                    this.btnLoading = false
                })
            } else {
                this.axios({
                    method: 'post',
                    url: '/dms/capex/submit',
                    data: params
                }).then(res => {
                    if (res.code === 200) {
                        this.$Message.success(res.msg)
                        this.$router.go(-1)
                        // location.reload()
                    } else {
                        this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
                    }
                }).finally(() => {
                    this.btnLoading = false
                })
            }

        },
        async onCache() {

            const params = await this.$refs.subProcessItem[0].getParams(true);

            if (!params) return;
            params.progressCode = this.progressCode
            params.id = this.id
            delete params.projectOwner
            delete params.submitByStr
            delete params.preparedByName

            if (params.cvInfo)
            {
                delete params.cvInfo.requestedBy
                delete params.cvInfo.submitByStr
            }

            if (params.scfInfoDto){
                delete params.scfInfoDto.submitByStr
            }

            if (params.downPaymentDto)
            {
                delete params.downPaymentDto.submitByStr
                delete params.downPaymentDto.preparedByName
                delete params.downPaymentDto.applyByName
            }

            if (params.capexCapitalizationInfoDTO){
                delete  params.capexCapitalizationInfoDTO.submitByStr
            }




            this.btnLoading = true

            this.axios({
                method: 'post',
                url: '/dms/capex/cachePONodeInfo',
                data: params
            }).then(res => {
                if (res.code === 200) {
                    this.$Message.success(res.msg)
                } else {
                    this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
                }
            }).finally(() => {
                this.btnLoading = false
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.bottom-handle-wrap {
    position: static;
}

.subProject {
    padding-bottom: 7px;
}
</style>
<style lang="scss">
.subProcess {
    border: solid 3px #035BEB;
    border-radius: 6px;
    margin-bottom: 16px;

    ::v-deep .ivu-tabs .ivu-tabs-tabpane {
        padding-top: 16px;
    }
}

</style>
